package com.cg;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.seeyon.ctp.common.AppContext;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.HttpClient;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.conf.OAConfig;
import com.dal.Common;
import com.db.DBConnection;
import com.seeyon.client.CTPRestClient;
import com.seeyon.client.CTPServiceClientManager;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class CGMainDal {

	public String sendForm(String json) {
		System.out.println("[sendForm]json = " + json);
		//创建连接
		Connection conn = new DBConnection().getDefConn();
		Statement stmt = null;
		try {
			stmt = conn.createStatement();

			HashMap<String, String> dept = new Common().initOA_Unit();
			//oa人员
			HashMap<String, String> member = new Common().initOA_Member();
			System.out.println("[sendForm]member = " + member);
			//oa单据实体
			HashMap<String, String> entity = new Common().initEntity();
			System.out.println("[sendForm]entity = " + entity);
			//将传输进来的json字符串转为json
			JSONObject object = JSONObject.parseObject(json);
			System.out.println("[sendForm]object = " + object.toJSONString());
			JSONObject data = new JSONObject();
			JSONArray dataSub = new JSONArray();
			JSONObject subItem;
			String bu = object.getString("BU") == null ? "" : object.getString("BU");
			System.out.println("[sendForm]bu = " + bu);
			String templateCode = "";
			if (bu.equals(OAConfig.OATJCODE)) {
				//取天津的CODE
				templateCode = OAConfig.cg_mbbh_tj;
			}
			if (bu.equals(OAConfig.OAFSCODE)) {
				//取佛山的CODE
				templateCode = OAConfig.cg_mbbh_fs;
			}
			if (bu.equals(OAConfig.OAXACODE)) {
				//取佛山的CODE
				templateCode = OAConfig.cg_mbbh_xa;
			}
			System.out.println("[sendForm]templateCode = " + templateCode);
			data.put("申请日期", object.getString("PRDate") == null ? "" : object.getString("PRDate"));
			data.put("采购申请号", object.getString("PRNo") == null ? "" : object.getString("PRNo"));
			data.put("申请部门", member.get(object.getString("Requestedby")) == null ?
					"" : ((String) member.get(object.getString("Requestedby"))).split(",")[2]);
			data.put("申请人", member.get(object.getString("Requestedby")) == null ?
					"" : ((String) member.get(object.getString("Requestedby"))).split(",")[0]);
			data.put("工号", object.getString("Requestedby") == null ?
					"" : object.getString("Requestedby"));
			data.put("申请人单位", member.get(object.getString("Requestedby")) == null ?
					"" : ((String) member.get(object.getString("Requestedby"))).split(",")[3]);
			data.put("填写人", member.get(object.getString("Requestedby")) == null ?
					"" : ((String) member.get(object.getString("Requestedby"))).split(",")[0]);
			data.put("填报人", member.get(object.getString("Requestor")) == null ?
					"" : ((String) member.get(object.getString("Requestor"))).split(",")[0]);
			data.put("成本中心号", object.getString("CCN") == null ? "" : object.getString("CCN"));
			data.put("用于", object.getString("UsedFor") == null ? "" : object.getString("UsedFor"));
			data.put("实体", object.getString("Entity") == null ? "" : object.getString("Entity"));
			data.put("选单位", entity.get(object.getString("Entity")) == null ? "" : entity.get(object.getString("Entity")));
			data.put("急单", object.getString("Urgent") == null ? "" : object.getString("Urgent"));
			data.put("是否固定资产", object.getString("Capital").equals("true") ? "固定资产" : "非固定资产");
			data.put("是否卖废品", object.getString("SaleScrap").equals("true") ? "卖废品" : "非卖废品");
			data.put("固定资产号", object.getString("InvestmentNo") == null ? "" : object.getString("InvestmentNo"));
			data.put("内部说明", object.getString("Explanation") == null ? "" : object.getString("Explanation"));
			data.put("是否紧急会签", (object.getString("IsPromiseOrder") != null && object.getString("IsPromiseOrder").equals("true"))
					? "是" : "否");
			if (object.getString("EHS") != null && object.getString("EHS").equals("true"))
				data.put("申请类型", "EHS");
			else if (object.getString("IT") != null && object.getString("IT").equals("true"))
				data.put("申请类型", "IT");
			else if (object.getString("Administrator") != null && object.getString("Administrator").equals("true"))
				data.put("申请类型", "Administrator");
			else if (object.getString("FK") != null && object.getString("FK").equals("true"))
				data.put("申请类型", "FK");
			data.put("预算总金额", object.getString("BudgetAmount") == null ? "" : object.getString("BudgetAmount"));
			data.put("部门内部编码", object.getString("CodebyDept") == null ? "" : object.getString("CodebyDept"));
			data.put("研发编码", object.getString("RDCode") == null ? "" : object.getString("RDCode"));
			data.put("质量编码", object.getString("QCode") == null ? "" : object.getString("QCode"));
			data.put("安全号", object.getString("SecurityCode") == null ? "" : object.getString("SecurityCode"));
			data.put("预估总金额", object.getString("EstimatedAmountMax") == null ? "" : object.getString("EstimatedAmountMax"));
			data.put("预估总金额范围", object.getString("EstimatedTotalAmountRange") == null ? "" : object.getString("EstimatedTotalAmountRange"));
			data.put("公司BU", bu);
			System.out.println("[sendForm]data = " + data.toJSONString());
			JSONArray list = object.getJSONArray("PRItems");
			for (int i = 0; i < list.size(); i++) {
				JSONObject item = list.getJSONObject(i);
				subItem = new JSONObject();
				subItem.put("物料代号", item.getString("PartNo") == null ? "" : item.getString("PartNo"));
				subItem.put("物料或劳务摘要", item.getString("Description") == null ? "" : item.getString("Description"));
				subItem.put("单位", item.getString("Unit") == null ? "" : item.getString("Unit"));
				subItem.put("数量", item.getString("Quantity") == null ? "" : item.getString("Quantity"));
				subItem.put("需求日期", item.getString("RequestedDate") == null ? "" : item.getString("RequestedDate"));
				subItem.put("固定资产预算号", item.getString("InvestmentNo") == null ? "" : item.getString("InvestmentNo"));
				subItem.put("备注", item.getString("AdditionalRemark") == null ? "" : item.getString("AdditionalRemark"));
				subItem.put("可否抵扣", item.getString("Deductable") == null ? "" : (item.getString("Deductable").equals("true") ? "是" : "否"));
				subItem.put("预算金额", item.getString("BudgetAmount") == null ? "" : item.getString("BudgetAmount"));
				dataSub.add(subItem);
			}
			data.put("sub", dataSub);
			System.out.println("[sendForm]sub = " + dataSub.toJSONString());
			CTPServiceClientManager clientManager = CTPServiceClientManager.getInstance(OAConfig.rest_ip);

			CTPRestClient client = clientManager.getRestClient();
			client.authenticate(OAConfig.rest_user, OAConfig.rest_pw);

			System.out.println("[sendForm]开始处理附件");
			//附件
			ArrayList attachments = new ArrayList();
			if (object.getString("Attachments") != null && !object.getString("Attachments").equals("")) {
				String[] ss = object.getString("Attachments").split(",");
				for (int i = 0; i < ss.length; i++) {
					try {
						attachments.add(Long.valueOf(Long.parseLong(ss[i])));
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			}
			System.out.println("[sendForm]附件处理完毕 attachments = " + attachments);
			if (member.get(object.getString("Requestor")) == null) {
				System.out.println("[sendForm]未匹配到发起人,流程发起失败.");
				return "未匹配到发起人,流程发起失败.";
			}
			Map map = new HashMap();
			//map.put("senderLoginName", "yysjs");//Admin@123
			map.put("senderLoginName", ((String) member.get(object.getString("Requestor"))).split(",")[1]);
			map.put("data", data.toJSONString());
			map.put("subject", "");
			map.put("param", "0");
			map.put("transfertype", "json");
			map.put("attachments", attachments);
			System.out.println("[sendForm]map：" + map);
			System.out.println("[sendForm]开始发起流程");
			//发起流程
			String result = client.post("flow/" + templateCode, map, String.class);
			System.out.println("[sendForm]开始发起流程，结果：" + result);
			if (result.equals("-1"))
				return "流程发起失败,请联系OA管理员检查日志ctp.log拉到最后向上搜索《" + templateCode + "》,查看报错信息.";

			return "流程发起成功.";
		} catch (Exception e) {
			e.printStackTrace();
			String str = "流程发起失败," + e.getMessage();
			return str;
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}


	public void sendResult() {
		//创建jdbc连接
		Connection conn = new DBConnection().getDefConn();
		Statement stmt = null;
		HashMap<String, String> map = new HashMap<String, String>();
		try {
			stmt = conn.createStatement();
			//查询协同表 连接formmain_0537  col_summary表状态为1或3, col_summary表是否发送为0,col_summary表表单id为-2804210610663174913
			//formmain_0537数据id为col_summary表的FORM_RECORDID（数据id） 并且去重
			try {
				//同步天津
				syncTJ(stmt);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				//同步佛山
				syncFS(stmt);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				//同步西安
				syncXA(stmt);
			} catch (Exception e) {
				e.printStackTrace();
			}

			try {
				//同步苏州
				syncSZ(stmt);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				//同步武汉
				syncWH(stmt);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				//同步广州
				syncGZ(stmt);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				//同步太原
				syncTY(stmt);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				//同步沈阳
				syncSY(stmt);
			} catch (Exception e) {
				e.printStackTrace();
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	/**
	 * 同步天津
	 *
	 * @param stmt
	 * @throws SQLException
	 */
	public void syncTJ(Statement stmt) throws SQLException {
		String sql = "SELECT " +
				"s.id, " +
				"s.state, " +
				"f.id mainId, " +
				"f.* " +
				"FROM col_summary s, " + OAConfig.cg_tableTJ + " f " +
				"WHERE s.isSend = 0 " +
				"AND s.state IN (1, 3) " +
				"AND s.FORM_APPID = " + OAConfig.cg_appidTJ + " " +
				"AND f.id = s.FORM_RECORDID";
		System.out.println("[syncTJ]同步天津 sql = " + sql);
		ResultSet ret = stmt.executeQuery(sql);
		JSONArray list = new JSONArray();
		JSONObject item;
		JSONObject obj;
		while (ret.next()) {
			item = new JSONObject();
			item.put("PRNo", ret.getString(OAConfig.cg_PRNoTJ));
			item.put("BU", ret.getString(OAConfig.BUTJ));
			item.put("oa_id", ret.getString("id"));
			item.put("oa_mainId", ret.getString("mainId"));
			item.put("oa_state", ret.getString("state"));
			item.put("Capital", (ret.getString(OAConfig.cg_CapitalTJ) != null && ret.getString(OAConfig.cg_CapitalTJ).equals("固定资产")) ? "true" : "false");
			item.put("InvestmentNo", ret.getString(OAConfig.cg_InvestmentNoTJ) == null ? "" : ret.getString(OAConfig.cg_InvestmentNoTJ));
			item.put("BudgetAmount", ret.getString(OAConfig.cg_BudgetAmountTJ) == null ? "" : ret.getString(OAConfig.cg_BudgetAmountTJ));
			item.put("attachment", ret.getString(OAConfig.cg_AttachmentTJ) == null ? "0" : ret.getString(OAConfig.cg_AttachmentTJ));
			//将查询到的数据存入数组中
			list.add(item);
		}
		System.out.println("[syncTJ]list：" + list.toJSONString());
		System.out.println("[syncTJ]size:" + list.size());
		//循环数组
		for (int i = 0; i < list.size(); i++) {
			item = list.getJSONObject(i);
			String billCode = item.getString("PRNo");
			String affId = item.getString("oa_id");
			String appCode = "";
			String creCode = "";
			try {
				//查询附件
				String attachmentSql = "select FILE_URL,FILENAME from CTP_ATTACHMENT where SUB_REFERENCE in (" + item.getString("oa_id") + "," + item.getString("attachment") + ")";
				System.out.println("[syncTJ]attachmentSql:" + attachmentSql);
				ret = stmt.executeQuery(attachmentSql);
				JSONArray atts = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//附件id
					obj.put("Id", ret.getString("FILE_URL"));
					//附件名称
					obj.put("Attachment", ret.getString("FILENAME"));
					obj.put("Remark", "");
					atts.add(obj);
				}
				System.out.println("[syncTJ]atts:" + atts.toJSONString());
				//将附件放入数组中
				list.getJSONObject(i).put("AttachmentList", atts);
				//查询评论信息
				String commentSql = "select m.code Processedby,a.receive_time ReceivedTime,c.CREATE_DATE ProcessedTime,c.EXT_ATT4 Approved,c.content Comments " +
						" from CTP_COMMENT_ALL c,ORG_MEMBER m,CTP_AFFAIR a " +
						" where m.id=c.CREATE_ID and a.id=c.AFFAIR_ID  and a.OBJECT_ID= " + item.getString("oa_id") +
						" order by a.CREATE_DATE";
				System.out.println("[syncTJ]commentSql:" + commentSql);
				ret = stmt.executeQuery(commentSql);
				JSONArray commons = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//人员编号
					obj.put("Processedby", ret.getString("Processedby") == null ? "" : ret.getString("Processedby"));
					//个人事项创建时间
					obj.put("ReceivedTime", ret.getString("ReceivedTime") == null ? "" : ret.getString("ReceivedTime"));
					//评论创建时间
					obj.put("ProcessedTime", ret.getString("ProcessedTime") == null ? "" : ret.getString("ProcessedTime"));
					//扩展字段 不知存的什么
					obj.put("Approved", ret.getString("Approved") == null ? "801" : (ret.getString("Approved").equals("disagree") ? "802" : "801"));
					//评论恢复信息
					obj.put("Comments", ret.getString("Comments") == null ? "" : ret.getString("Comments"));
					commons.add(obj);
				}
				System.out.println("[syncTJ]commons:" + commons.toJSONString());
				//将评论信息放入数组中
				list.getJSONObject(i).put("ProcessList", commons);
				//查询formson_0538表 应该是子表
				String sql2 = "select " +
						OAConfig.SON_ItemNoTJ + " ItemNo," +
						OAConfig.SON_InvestmentNoTJ + " InvestmentNo," +
						OAConfig.SON_BudgetAmountTJ + " BudgetAmount " +
						"from " + OAConfig.cg_sontableTJ + " " +
						"where formmain_id=" + item.getString("oa_mainId");
				System.out.println("[syncTJ]同步天津 sql2 = " + sql2);
				ret = stmt.executeQuery(sql2);
				JSONArray ItemList = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//序号
					obj.put("ItemNo", ret.getString("ItemNo") == null ? "" : ret.getString("ItemNo"));
					//看数据字典
					obj.put("InvestmentNo", ret.getString("InvestmentNo") == null ? "" : ret.getString("InvestmentNo"));
					//看数据字典
					obj.put("BudgetAmount", ret.getString("BudgetAmount") == null ? "" : ret.getString("BudgetAmount"));
					ItemList.add(obj);
				}
				list.getJSONObject(i).put("ItemList", ItemList);
				System.out.println("[syncTJ]doRequest send:" + list.getJSONObject(i));

				String sql3 = "select a.field0004,b.MEMBER_ID,b.id from " + OAConfig.cg_tableTJ + " a left join CTP_AFFAIR b on a.id = b.FORM_RECORDID where a.id = '" + item.getString("oa_mainId") + "'";
				System.out.println("[syncTJ]同步天津 sql3 = " + sql3);
				ret = stmt.executeQuery(sql3);
				while (ret.next()) {
					creCode = ret.getString("MEMBER_ID");
                    appCode = ret.getString("field0004");
				}
				String result = doRequest(list.getJSONObject(i),billCode,affId,appCode,creCode,0);

				System.out.println("[syncTJ]doRequest result:" + result);
				//"+result+"
				stmt.executeUpdate("update col_summary set isSend='1' where id=" + item.getString("oa_id"));
				stmt.executeUpdate("update formmain_0537 set field0082='" + result + "' where id=" + item.getString("oa_mainId"));
			} catch (Exception e) {
				System.out.println("[syncTJ]Exception:" + e.getMessage());
				// TODO: handle exception
				e.printStackTrace();
				stmt.executeUpdate("update col_summary set isSend='1' where id=" + item.getString("oa_id"));
				stmt.executeUpdate("update formmain_0537 set field0082='" + e.getMessage() + "' where id=" + item.getString("oa_mainId"));
			}
		}
	}

	/**
	 * 同步佛山
	 *
	 * @param stmt
	 * @throws SQLException
	 */
	public void syncFS(Statement stmt) throws SQLException {
		String sql = "SELECT " +
				"s.id, " +
				"s.state, " +
				"f.id mainId, " +
				"f.* " +
				"FROM col_summary s, " + OAConfig.cg_tableFS + " f " +
				"WHERE s.isSend = 0 " +
				"AND s.state IN (1, 3) " +
				"AND s.FORM_APPID = " + OAConfig.cg_appidFS + " " +
				"AND f.id = s.FORM_RECORDID";
		System.out.println("[syncFs]同步佛山 sql = " + sql);
		ResultSet ret = stmt.executeQuery(sql);
		JSONArray list = new JSONArray();
		JSONObject item;
		JSONObject obj;
		while (ret.next()) {
			item = new JSONObject();
			item.put("PRNo", ret.getString(OAConfig.cg_PRNoFS));
			item.put("BU", ret.getString(OAConfig.BUFS));
			item.put("oa_id", ret.getString("id"));
			item.put("oa_mainId", ret.getString("mainId"));
			item.put("oa_state", ret.getString("state"));
			item.put("Capital", (ret.getString(OAConfig.cg_CapitalFS) != null && ret.getString(OAConfig.cg_CapitalFS).equals("固定资产")) ? "true" : "false");
			item.put("InvestmentNo", ret.getString(OAConfig.cg_InvestmentNoFS) == null ? "" : ret.getString(OAConfig.cg_InvestmentNoFS));
			item.put("BudgetAmount", ret.getString(OAConfig.cg_BudgetAmountFS) == null ? "" : ret.getString(OAConfig.cg_BudgetAmountFS));
			item.put("attachment", ret.getString(OAConfig.cg_AttachmentFS) == null ? "0" : ret.getString(OAConfig.cg_AttachmentFS));
			//将查询到的数据存入数组中
			list.add(item);
		}
		System.out.println("[syncFs]list：" + list.toJSONString());
		System.out.println("[syncFs]size:" + list.size());

		//循环数组
		for (int i = 0; i < list.size(); i++) {
			item = list.getJSONObject(i);
			String billCode = item.getString("PRNo");
			String affId = item.getString("oa_id");
			String appCode = "";
			String creCode = "";
			try {
				//查询附件
				String attachmentSql = "select FILE_URL,FILENAME from CTP_ATTACHMENT where SUB_REFERENCE in (" + item.getString("oa_id") + "," + item.getString("attachment") + ")";
				System.out.println("[syncFs]attachmentSql:" + attachmentSql);
				ret = stmt.executeQuery(attachmentSql);
				JSONArray atts = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//附件id
					obj.put("Id", ret.getString("FILE_URL"));
					//附件名称
					obj.put("Attachment", ret.getString("FILENAME"));
					obj.put("Remark", "");
					atts.add(obj);
				}
				System.out.println("[syncFs]atts:" + atts.toJSONString());

				//将附件放入数组中
				list.getJSONObject(i).put("AttachmentList", atts);
				//查询评论信息
				String commentSql = "select m.code Processedby,a.receive_time ReceivedTime,c.CREATE_DATE ProcessedTime,c.EXT_ATT4 Approved,c.content Comments " +
						" from CTP_COMMENT_ALL c,ORG_MEMBER m,CTP_AFFAIR a " +
						" where m.id=c.CREATE_ID and a.id=c.AFFAIR_ID  and a.OBJECT_ID= " + item.getString("oa_id") +
						" order by a.CREATE_DATE";
				System.out.println("[syncFs]commentSql:" + commentSql);
				ret = stmt.executeQuery(commentSql);
				JSONArray commons = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//人员编号
					obj.put("Processedby", ret.getString("Processedby") == null ? "" : ret.getString("Processedby"));
					//个人事项创建时间
					obj.put("ReceivedTime", ret.getString("ReceivedTime") == null ? "" : ret.getString("ReceivedTime"));
					//评论创建时间
					obj.put("ProcessedTime", ret.getString("ProcessedTime") == null ? "" : ret.getString("ProcessedTime"));
					//扩展字段 不知存的什么
					obj.put("Approved", ret.getString("Approved") == null ? "801" : (ret.getString("Approved").equals("disagree") ? "802" : "801"));
					//评论恢复信息
					obj.put("Comments", ret.getString("Comments") == null ? "" : ret.getString("Comments"));
					commons.add(obj);
				}
				System.out.println("[syncFs]commons:" + commons.toJSONString());

				//将评论信息放入数组中
				list.getJSONObject(i).put("ProcessList", commons);
				//查询formson_0538表 应该是子表
				String sql2 = "select " +
						OAConfig.SON_ItemNoFS + " ItemNo," +
						OAConfig.SON_InvestmentNoFS + " InvestmentNo," +
						OAConfig.SON_BudgetAmountFS + " BudgetAmount " +
						"from " + OAConfig.cg_sontableFS + " " +
						"where formmain_id=" + item.getString("oa_mainId");
				System.out.println("[syncFs]同步佛山 sql2 = " + sql2);

				ret = stmt.executeQuery(sql2);
				JSONArray ItemList = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//序号
					obj.put("ItemNo", ret.getString("ItemNo") == null ? "" : ret.getString("ItemNo"));
					//看数据字典
					obj.put("InvestmentNo", ret.getString("InvestmentNo") == null ? "" : ret.getString("InvestmentNo"));
					//看数据字典
					obj.put("BudgetAmount", ret.getString("BudgetAmount") == null ? "" : ret.getString("BudgetAmount"));
					ItemList.add(obj);
				}
				list.getJSONObject(i).put("ItemList", ItemList);
				System.out.println("[syncFs]doRequest send:" + list.getJSONObject(i));

				String sql3 = "select a.field0004,b.MEMBER_ID,b.id from "+ OAConfig.cg_tableFS + " a left join CTP_AFFAIR b on a.id = b.FORM_RECORDID where a.id = '" + item.getString("oa_mainId") + "'";
				System.out.println("[syncFs]同步佛山 sql3 = " + sql3);
				ret = stmt.executeQuery(sql3);
				while (ret.next()) {
					creCode = ret.getString("MEMBER_ID");
					appCode = ret.getString("field0004");
				}
				String result = doRequest(list.getJSONObject(i),billCode,affId,appCode,creCode,0);

				System.out.println("[syncFs]doRequest result:" + result);
				//"+result+"
				stmt.executeUpdate("update col_summary set isSend='1' where id=" + item.getString("oa_id"));
				stmt.executeUpdate("update formmain_1145 set field0082='" + result + "' where id=" + item.getString("oa_mainId"));
			} catch (Exception e) {
				System.out.println("[syncFs]Exception:" + e.getMessage());
				// TODO: handle exception
				e.printStackTrace();
				stmt.executeUpdate("update col_summary set isSend='1' where id=" + item.getString("oa_id"));
				stmt.executeUpdate("update formmain_1145 set field0082='" + e.getMessage() + "' where id=" + item.getString("oa_mainId"));
			}
		}
	}

	/**
	 * 同步西安
	 *
	 * @param stmt
	 * @throws SQLException
	 */
	public void syncXA(Statement stmt) throws SQLException {
		String sql = "SELECT " +
				"s.id, " +
				"s.state, " +
				"f.id mainId, " +
				"f.* " +
				"FROM col_summary s, " + OAConfig.cg_tableXA + " f " +
				"WHERE s.isSend = 0 " +
				"AND s.state IN (1, 3) " +
				"AND s.FORM_APPID = " + OAConfig.cg_appidXA + " " +
				"AND f.id = s.FORM_RECORDID";
		System.out.println("[syncXA]同步西安 sql = " + sql);
		ResultSet ret = stmt.executeQuery(sql);
		JSONArray list = new JSONArray();
		JSONObject item;
		JSONObject obj;
		while (ret.next()) {
			item = new JSONObject();
			item.put("PRNo", ret.getString(OAConfig.cg_PRNoXA));
			item.put("BU", ret.getString(OAConfig.BUXA));
			item.put("oa_id", ret.getString("id"));
			item.put("oa_mainId", ret.getString("mainId"));
			item.put("oa_state", ret.getString("state"));
			item.put("Capital", (ret.getString(OAConfig.cg_CapitalXA) != null && ret.getString(OAConfig.cg_CapitalXA).equals("固定资产")) ? "true" : "false");
			item.put("InvestmentNo", ret.getString(OAConfig.cg_InvestmentNoXA) == null ? "" : ret.getString(OAConfig.cg_InvestmentNoXA));
			item.put("BudgetAmount", ret.getString(OAConfig.cg_BudgetAmountXA) == null ? "" : ret.getString(OAConfig.cg_BudgetAmountXA));
			item.put("SecurityCode", ret.getString(OAConfig.cg_SecurityCodeXA) == null ? "" : ret.getString(OAConfig.cg_SecurityCodeXA));
			item.put("attachment", ret.getString(OAConfig.cg_AttachmentXA) == null ? "0" : ret.getString(OAConfig.cg_AttachmentXA));
			//将查询到的数据存入数组中
			list.add(item);
		}
		System.out.println("[syncXA]list：" + list.toJSONString());
		System.out.println("[syncXA]size:" + list.size());

		//循环数组
		for (int i = 0; i < list.size(); i++) {
			item = list.getJSONObject(i);
			String billCode = item.getString("PRNo");
			String affId = item.getString("oa_id");
			String appCode = "";
			String creCode = "";
			try {
				//查询附件
				String attachmentSql = "select FILE_URL,FILENAME from CTP_ATTACHMENT where SUB_REFERENCE in (" + item.getString("oa_id") + "," + item.getString("attachment") + ")";
				System.out.println("[syncXA]attachmentSql:" + attachmentSql);
				ret = stmt.executeQuery(attachmentSql);
				JSONArray atts = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//附件id
					obj.put("Id", ret.getString("FILE_URL"));
					//附件名称
					obj.put("Attachment", ret.getString("FILENAME"));
					obj.put("Remark", "");
					atts.add(obj);
				}
				System.out.println("[syncXA]atts:" + atts.toJSONString());

				//将附件放入数组中
				list.getJSONObject(i).put("AttachmentList", atts);
				//查询评论信息
				String commentSql = "select c.id id,m.code Processedby,a.receive_time ReceivedTime,c.CREATE_DATE ProcessedTime,c.EXT_ATT4 Approved,c.content Comments " +
						" from CTP_COMMENT_ALL c,ORG_MEMBER m,CTP_AFFAIR a " +
						" where m.id=c.CREATE_ID and a.id=c.AFFAIR_ID  and a.OBJECT_ID= " + item.getString("oa_id") +
						" order by a.CREATE_DATE";
				System.out.println("[syncXA]commentSql:" + commentSql);
				ret = stmt.executeQuery(commentSql);
				JSONArray commons = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//人员编号
					obj.put("Processedby", ret.getString("Processedby") == null ? "" : ret.getString("Processedby"));
					//个人事项创建时间
					obj.put("ReceivedTime", ret.getString("ReceivedTime") == null ? "" : ret.getString("ReceivedTime"));
					//评论创建时间
					obj.put("ProcessedTime", ret.getString("ProcessedTime") == null ? "" : ret.getString("ProcessedTime"));
					//扩展字段 不知存的什么
					obj.put("Approved", ret.getString("Approved") == null ? "801" : (ret.getString("Approved").equals("disagree") ? "802" : "801"));
					//评论恢复信息
					obj.put("Comments", ret.getString("Comments") == null ? "" : ret.getString("Comments"));
					commons.add(obj);
				}
				System.out.println("[syncXA]commons:" + commons.toJSONString());

				//将评论信息放入数组中
				list.getJSONObject(i).put("ProcessList", commons);
				//查询it选型
				String itsql = "select " +
						OAConfig.SON_ItxhXA + " xh," +
						OAConfig.SON_ItxxXA + " xx " +
				"from " + OAConfig.cg_sontableITXA + " where formmain_id = " + item.getString("oa_mainId");
				System.out.println("[syncXA]同步西安 itsql = " + itsql);
				ret = stmt.executeQuery(itsql);

				JSONObject itjson = new JSONObject();
				while (ret.next()) {
					if(ret.getString("xh") != null){
						itjson.put(ret.getString("xh"), ret.getString("xx") == null ? "" : ret.getString("xx"));
					}
				}


				//查询formson_0538表 应该是子表
				String sql2 = "select " +
						OAConfig.SON_ItemNoXA + " ItemNo," +
						OAConfig.SON_InvestmentNoXA + " InvestmentNo," +
						OAConfig.SON_BudgetAmountXA + " BudgetAmount " +
						"from " + OAConfig.cg_sontableXA + " " +
						"where formmain_id=" + item.getString("oa_mainId");
				System.out.println("[syncXA]同步西安 sql2 = " + sql2);

				ret = stmt.executeQuery(sql2);
				JSONArray ItemList = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//序号
					obj.put("ItemNo", ret.getString("ItemNo") == null ? "" : ret.getString("ItemNo"));
					//看数据字典
					obj.put("InvestmentNo", ret.getString("InvestmentNo") == null ? "" : ret.getString("InvestmentNo"));
					//看数据字典
					obj.put("BudgetAmount", ret.getString("BudgetAmount") == null ? "" : ret.getString("BudgetAmount"));
					//it选型
					if(!itjson.isEmpty()){
						obj.put("ITSelection", itjson.getString(ret.getString("ItemNo")));
					}
					ItemList.add(obj);
				}
				list.getJSONObject(i).put("ItemList", ItemList);
				System.out.println("[syncXA]doRequest send:" + list.getJSONObject(i));

				String sql3 = "select a.field0004,b.MEMBER_ID,b.id from "+ OAConfig.cg_tableXA + " a left join CTP_AFFAIR b on a.id = b.FORM_RECORDID where a.id = '" + item.getString("oa_mainId") + "'";
				System.out.println("[syncXA]同步西安 sql3 = " + sql3);
				ret = stmt.executeQuery(sql3);
				while (ret.next()) {
					creCode = ret.getString("MEMBER_ID");
					appCode = ret.getString("field0004");
				}
				String result = doRequest(list.getJSONObject(i),billCode,affId,appCode,creCode,1);
				System.out.println("[syncXA]doRequest result:" + result);
				//"+result+"
				stmt.executeUpdate("update col_summary set isSend='1' where id=" + item.getString("oa_id"));
				stmt.executeUpdate("update formmain_1145 set field0082='" + result + "' where id=" + item.getString("oa_mainId"));
			} catch (Exception e) {
				System.out.println("[syncXA]Exception:" + e.getMessage());
				// TODO: handle exception
				e.printStackTrace();
				stmt.executeUpdate("update col_summary set isSend='1' where id=" + item.getString("oa_id"));
				stmt.executeUpdate("update formmain_1145 set field0082='" + e.getMessage() + "' where id=" + item.getString("oa_mainId"));
			}
		}
	}

	/**
	 * 同步苏州
	 *
	 * @param stmt
	 * @throws SQLException
	 */
	public void syncSZ(Statement stmt) throws SQLException {
		String sql = "SELECT " +
				"s.id, " +
				"s.state, " +
				"f.id mainId, " +
				"f.* " +
				"FROM col_summary s, " + OAConfig.cg_tableSZ + " f " +
				"WHERE s.isSend = 0 " +
				"AND s.state IN (1, 3) " +
				"AND s.FORM_APPID = " + OAConfig.cg_appidSZ + " " +
				"AND f.id = s.FORM_RECORDID";
		System.out.println("[syncFs]同步苏州 sql = " + sql);
		ResultSet ret = stmt.executeQuery(sql);
		JSONArray list = new JSONArray();
		JSONObject item;
		JSONObject obj;
		while (ret.next()) {
			item = new JSONObject();
			item.put("PRNo", ret.getString(OAConfig.cg_PRNoSZ));
			item.put("BU", ret.getString(OAConfig.BUSZ));
			item.put("oa_id", ret.getString("id"));
			item.put("oa_mainId", ret.getString("mainId"));
			item.put("oa_state", ret.getString("state"));
			item.put("Capital", (ret.getString(OAConfig.cg_CapitalSZ) != null && ret.getString(OAConfig.cg_CapitalSZ).equals("固定资产")) ? "true" : "false");
			item.put("InvestmentNo", ret.getString(OAConfig.cg_InvestmentNoSZ) == null ? "" : ret.getString(OAConfig.cg_InvestmentNoSZ));
			item.put("BudgetAmount", ret.getString(OAConfig.cg_BudgetAmountSZ) == null ? "" : ret.getString(OAConfig.cg_BudgetAmountSZ));
			item.put("SecurityCode", ret.getString(OAConfig.cg_SecurityCodeSZ) == null ? "" : ret.getString(OAConfig.cg_SecurityCodeSZ));
			item.put("attachment", ret.getString(OAConfig.cg_AttachmentSZ) == null ? "0" : ret.getString(OAConfig.cg_AttachmentSZ));
			//将查询到的数据存入数组中
			list.add(item);
		}
		System.out.println("[syncSZ]list：" + list.toJSONString());
		System.out.println("[syncSZ]size:" + list.size());

		//循环数组
		for (int i = 0; i < list.size(); i++) {
			item = list.getJSONObject(i);
			String billCode = item.getString("PRNo");
			String affId = item.getString("oa_id");
			String appCode = "";
			String creCode = "";
			try {
				//查询附件
				String attachmentSql = "select FILE_URL,FILENAME from CTP_ATTACHMENT where SUB_REFERENCE in (" + item.getString("oa_id") + "," + item.getString("attachment") + ")";
				System.out.println("[syncSZ]attachmentSql:" + attachmentSql);
				ret = stmt.executeQuery(attachmentSql);
				JSONArray atts = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//附件id
					obj.put("Id", ret.getString("FILE_URL"));
					//附件名称
					obj.put("Attachment", ret.getString("FILENAME"));
					obj.put("Remark", "");
					atts.add(obj);
				}
				System.out.println("[syncSZ]atts:" + atts.toJSONString());

				//将附件放入数组中
				list.getJSONObject(i).put("AttachmentList", atts);
				//查询评论信息
				String commentSql = "select c.id id,m.code Processedby,a.receive_time ReceivedTime,c.CREATE_DATE ProcessedTime,c.EXT_ATT4 Approved,c.content Comments " +
						" from CTP_COMMENT_ALL c,ORG_MEMBER m,CTP_AFFAIR a " +
						" where m.id=c.CREATE_ID and a.id=c.AFFAIR_ID  and a.OBJECT_ID= " + item.getString("oa_id") +
						" order by a.CREATE_DATE";
				System.out.println("[syncSZ]commentSql:" + commentSql);
				ret = stmt.executeQuery(commentSql);
				JSONArray commons = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//人员编号
					obj.put("Processedby", ret.getString("Processedby") == null ? "" : ret.getString("Processedby"));
					//个人事项创建时间
					obj.put("ReceivedTime", ret.getString("ReceivedTime") == null ? "" : ret.getString("ReceivedTime"));
					//评论创建时间
					obj.put("ProcessedTime", ret.getString("ProcessedTime") == null ? "" : ret.getString("ProcessedTime"));
					//扩展字段 不知存的什么
					obj.put("Approved", ret.getString("Approved") == null ? "801" : (ret.getString("Approved").equals("disagree") ? "802" : "801"));
					//评论恢复信息
					obj.put("Comments", ret.getString("Comments") == null ? "" : ret.getString("Comments"));
					commons.add(obj);
				}
				System.out.println("[syncSZ]commons:" + commons.toJSONString());

				//将评论信息放入数组中
				list.getJSONObject(i).put("ProcessList", commons);
				//查询it选型
				String itsql = "select " +
						OAConfig.SON_ItxhSZ + " xh," +
						OAConfig.SON_ItxxSZ + " xx " +
						"from " + OAConfig.cg_sontableITSZ + " where formmain_id = " + item.getString("oa_mainId");
				System.out.println("[syncSZ]同步苏州 itsql = " + itsql);
				ret = stmt.executeQuery(itsql);

				JSONObject itjson = new JSONObject();
				while (ret.next()) {
					if(ret.getString("xh") != null){
						itjson.put(ret.getString("xh"), ret.getString("xx") == null ? "" : ret.getString("xx"));
					}
				}


				//查询formson_0538表 应该是子表
				String sql2 = "select " +
						OAConfig.SON_ItemNoSZ + " ItemNo," +
						OAConfig.SON_InvestmentNoSZ + " InvestmentNo," +
						OAConfig.SON_BudgetAmountSZ + " BudgetAmount " +
						"from " + OAConfig.cg_sontableSZ + " " +
						"where formmain_id=" + item.getString("oa_mainId");
				System.out.println("[syncSZ]同步苏州 sql2 = " + sql2);

				ret = stmt.executeQuery(sql2);
				JSONArray ItemList = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//序号
					obj.put("ItemNo", ret.getString("ItemNo") == null ? "" : ret.getString("ItemNo"));
					//看数据字典
					obj.put("InvestmentNo", ret.getString("InvestmentNo") == null ? "" : ret.getString("InvestmentNo"));
					//看数据字典
					obj.put("BudgetAmount", ret.getString("BudgetAmount") == null ? "" : ret.getString("BudgetAmount"));
					//it选型
					if(!itjson.isEmpty()){
						obj.put("ITSelection", itjson.getString(ret.getString("ItemNo")));
					}
					ItemList.add(obj);
				}
				list.getJSONObject(i).put("ItemList", ItemList);
				System.out.println("[syncFsSZ]doRequest send:" + list.getJSONObject(i));

				String sql3 = "select a.field0004,b.MEMBER_ID,b.id from "+ OAConfig.cg_tableSZ + " a left join CTP_AFFAIR b on a.id = b.FORM_RECORDID where a.id = '" + item.getString("oa_mainId") + "'";
				System.out.println("[syncSZ]同步苏州 sql3 = " + sql3);
				ret = stmt.executeQuery(sql3);
				while (ret.next()) {
					creCode = ret.getString("MEMBER_ID");
					appCode = ret.getString("field0004");
				}
				String result = doRequest(list.getJSONObject(i),billCode,affId,appCode,creCode,1);
				System.out.println("[syncSZ]doRequest result:" + result);
				//"+result+"
				stmt.executeUpdate("update col_summary set isSend='1' where id=" + item.getString("oa_id"));
				stmt.executeUpdate("update formmain_1145 set field0082='" + result + "' where id=" + item.getString("oa_mainId"));
			} catch (Exception e) {
				System.out.println("[syncSZ]Exception:" + e.getMessage());
				// TODO: handle exception
				e.printStackTrace();
				stmt.executeUpdate("update col_summary set isSend='1' where id=" + item.getString("oa_id"));
				stmt.executeUpdate("update formmain_1145 set field0082='" + e.getMessage() + "' where id=" + item.getString("oa_mainId"));
			}
		}
	}

	/**
	 * 同步武汉
	 *
	 * @param stmt
	 * @throws SQLException
	 */
	public void syncWH(Statement stmt) throws SQLException {
		String sql = "SELECT " +
				"s.id, " +
				"s.state, " +
				"f.id mainId, " +
				"f.* " +
				"FROM col_summary s, " + OAConfig.cg_tableWH + " f " +
				"WHERE s.isSend = 0 " +
				"AND s.state IN (1, 3) " +
				"AND s.FORM_APPID = " + OAConfig.cg_appidWH + " " +
				"AND f.id = s.FORM_RECORDID";
		System.out.println("[syncWH]同步武汉 sql = " + sql);
		ResultSet ret = stmt.executeQuery(sql);
		JSONArray list = new JSONArray();
		JSONObject item;
		JSONObject obj;
		while (ret.next()) {
			item = new JSONObject();
			item.put("PRNo", ret.getString(OAConfig.cg_PRNoWH));
			item.put("BU", ret.getString(OAConfig.BUWH));
			item.put("oa_id", ret.getString("id"));
			item.put("oa_mainId", ret.getString("mainId"));
			item.put("oa_state", ret.getString("state"));
			item.put("Capital", (ret.getString(OAConfig.cg_CapitalWH) != null && ret.getString(OAConfig.cg_CapitalWH).equals("固定资产")) ? "true" : "false");
			item.put("InvestmentNo", ret.getString(OAConfig.cg_InvestmentNoWH) == null ? "" : ret.getString(OAConfig.cg_InvestmentNoWH));
			item.put("BudgetAmount", ret.getString(OAConfig.cg_BudgetAmountWH) == null ? "" : ret.getString(OAConfig.cg_BudgetAmountWH));
			item.put("SecurityCode", ret.getString(OAConfig.cg_SecurityCodeWH) == null ? "" : ret.getString(OAConfig.cg_SecurityCodeWH));
			item.put("attachment", ret.getString(OAConfig.cg_AttachmentWH) == null ? "0" : ret.getString(OAConfig.cg_AttachmentWH));
			//将查询到的数据存入数组中
			list.add(item);
		}
		System.out.println("[syncWH]list：" + list.toJSONString());
		System.out.println("[syncWH]size:" + list.size());

		//循环数组
		for (int i = 0; i < list.size(); i++) {
			item = list.getJSONObject(i);
			String billCode = item.getString("PRNo");
			String affId = item.getString("oa_id");
			String appCode = "";
			String creCode = "";
			try {
				//查询附件
				String attachmentSql = "select FILE_URL,FILENAME from CTP_ATTACHMENT where SUB_REFERENCE in (" + item.getString("oa_id") + "," + item.getString("attachment") + ")";
				System.out.println("[syncWH]attachmentSql:" + attachmentSql);
				ret = stmt.executeQuery(attachmentSql);
				JSONArray atts = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//附件id
					obj.put("Id", ret.getString("FILE_URL"));
					//附件名称
					obj.put("Attachment", ret.getString("FILENAME"));
					obj.put("Remark", "");
					atts.add(obj);
				}
				System.out.println("[syncWH]atts:" + atts.toJSONString());

				//将附件放入数组中
				list.getJSONObject(i).put("AttachmentList", atts);
				//查询评论信息
				String commentSql = "select c.id id,m.code Processedby,a.receive_time ReceivedTime,c.CREATE_DATE ProcessedTime,c.EXT_ATT4 Approved,c.content Comments " +
						" from CTP_COMMENT_ALL c,ORG_MEMBER m,CTP_AFFAIR a " +
						" where m.id=c.CREATE_ID and a.id=c.AFFAIR_ID  and a.OBJECT_ID= " + item.getString("oa_id") +
						" order by a.CREATE_DATE";
				System.out.println("[syncWH]commentSql:" + commentSql);
				ret = stmt.executeQuery(commentSql);
				JSONArray commons = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//人员编号
					obj.put("Processedby", ret.getString("Processedby") == null ? "" : ret.getString("Processedby"));
					//个人事项创建时间
					obj.put("ReceivedTime", ret.getString("ReceivedTime") == null ? "" : ret.getString("ReceivedTime"));
					//评论创建时间
					obj.put("ProcessedTime", ret.getString("ProcessedTime") == null ? "" : ret.getString("ProcessedTime"));
					//扩展字段 不知存的什么
					obj.put("Approved", ret.getString("Approved") == null ? "801" : (ret.getString("Approved").equals("disagree") ? "802" : "801"));
					//评论恢复信息
					obj.put("Comments", ret.getString("Comments") == null ? "" : ret.getString("Comments"));
					commons.add(obj);
				}
				System.out.println("[syncWH]commons:" + commons.toJSONString());

				//将评论信息放入数组中
				list.getJSONObject(i).put("ProcessList", commons);
				//查询it选型
				String itsql = "select " +
						OAConfig.SON_ItxhWH + " xh," +
						OAConfig.SON_ItxxWH + " xx " +
						"from " + OAConfig.cg_sontableITWH + " where formmain_id = " + item.getString("oa_mainId");
				System.out.println("[syncWH]同步武汉 itsql = " + itsql);
				ret = stmt.executeQuery(itsql);

				JSONObject itjson = new JSONObject();
				while (ret.next()) {
					if(ret.getString("xh") != null){
						itjson.put(ret.getString("xh"), ret.getString("xx") == null ? "" : ret.getString("xx"));
					}
				}


				//查询formson_0538表 应该是子表
				String sql2 = "select " +
						OAConfig.SON_ItemNoWH + " ItemNo," +
						OAConfig.SON_InvestmentNoWH + " InvestmentNo," +
						OAConfig.SON_BudgetAmountWH + " BudgetAmount " +
						"from " + OAConfig.cg_sontableWH + " " +
						"where formmain_id=" + item.getString("oa_mainId");
				System.out.println("[syncWH]同步武汉 sql2 = " + sql2);

				ret = stmt.executeQuery(sql2);
				JSONArray ItemList = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//序号
					obj.put("ItemNo", ret.getString("ItemNo") == null ? "" : ret.getString("ItemNo"));
					//看数据字典
					obj.put("InvestmentNo", ret.getString("InvestmentNo") == null ? "" : ret.getString("InvestmentNo"));
					//看数据字典
					obj.put("BudgetAmount", ret.getString("BudgetAmount") == null ? "" : ret.getString("BudgetAmount"));
					//it选型
					if(!itjson.isEmpty()){
						obj.put("ITSelection", itjson.getString(ret.getString("ItemNo")));
					}
					ItemList.add(obj);
				}
				list.getJSONObject(i).put("ItemList", ItemList);
				System.out.println("[syncWH]doRequest send:" + list.getJSONObject(i));

				String sql3 = "select a.field0004,b.MEMBER_ID,b.id from "+ OAConfig.cg_tableWH + " a left join CTP_AFFAIR b on a.id = b.FORM_RECORDID where a.id = '" + item.getString("oa_mainId") + "'";
				System.out.println("[syncWH]同步武汉 sql3 = " + sql3);
				ret = stmt.executeQuery(sql3);
				while (ret.next()) {
					creCode = ret.getString("MEMBER_ID");
					appCode = ret.getString("field0004");
				}
				String result = doRequest(list.getJSONObject(i),billCode,affId,appCode,creCode,1);
				System.out.println("[syncWH]doRequest result:" + result);
				//"+result+"
				stmt.executeUpdate("update col_summary set isSend='1' where id=" + item.getString("oa_id"));
				stmt.executeUpdate("update formmain_1145 set field0082='" + result + "' where id=" + item.getString("oa_mainId"));
			} catch (Exception e) {
				System.out.println("[syncWH]Exception:" + e.getMessage());
				// TODO: handle exception
				e.printStackTrace();
				stmt.executeUpdate("update col_summary set isSend='1' where id=" + item.getString("oa_id"));
				stmt.executeUpdate("update formmain_1145 set field0082='" + e.getMessage() + "' where id=" + item.getString("oa_mainId"));
			}
		}
	}

	/**
	 * 同步广州
	 *
	 * @param stmt
	 * @throws SQLException
	 */
	public void syncGZ(Statement stmt) throws SQLException {
		String sql = "SELECT " +
				"s.id, " +
				"s.state, " +
				"f.id mainId, " +
				"f.* " +
				"FROM col_summary s, " + OAConfig.cg_tableGZ + " f " +
				"WHERE s.isSend = 0 " +
				"AND s.state IN (1, 3) " +
				"AND s.FORM_APPID = " + OAConfig.cg_appidGZ + " " +
				"AND f.id = s.FORM_RECORDID";
		System.out.println("[syncGZ]同步广州 sql = " + sql);
		ResultSet ret = stmt.executeQuery(sql);
		JSONArray list = new JSONArray();
		JSONObject item;
		JSONObject obj;
		while (ret.next()) {
			item = new JSONObject();
			item.put("PRNo", ret.getString(OAConfig.cg_PRNoGZ));
			item.put("BU", ret.getString(OAConfig.BUGZ));
			item.put("oa_id", ret.getString("id"));
			item.put("oa_mainId", ret.getString("mainId"));
			item.put("oa_state", ret.getString("state"));
			item.put("Capital", (ret.getString(OAConfig.cg_CapitalGZ) != null && ret.getString(OAConfig.cg_CapitalGZ).equals("固定资产")) ? "true" : "false");
			item.put("InvestmentNo", ret.getString(OAConfig.cg_InvestmentNoGZ) == null ? "" : ret.getString(OAConfig.cg_InvestmentNoGZ));
			item.put("BudgetAmount", ret.getString(OAConfig.cg_BudgetAmountGZ) == null ? "" : ret.getString(OAConfig.cg_BudgetAmountGZ));
			item.put("SecurityCode", ret.getString(OAConfig.cg_SecurityCodeGZ) == null ? "" : ret.getString(OAConfig.cg_SecurityCodeGZ));
			item.put("attachment", ret.getString(OAConfig.cg_AttachmentGZ) == null ? "0" : ret.getString(OAConfig.cg_AttachmentGZ));
			//将查询到的数据存入数组中
			list.add(item);
		}
		System.out.println("[syncGZ]list：" + list.toJSONString());
		System.out.println("[syncGZ]size:" + list.size());

		//循环数组
		for (int i = 0; i < list.size(); i++) {
			item = list.getJSONObject(i);
			String billCode = item.getString("PRNo");
			String affId = item.getString("oa_id");
			String appCode = "";
			String creCode = "";
			try {
				//查询附件
				String attachmentSql = "select FILE_URL,FILENAME from CTP_ATTACHMENT where SUB_REFERENCE in (" + item.getString("oa_id") + "," + item.getString("attachment") + ")";
				System.out.println("[syncGZ]attachmentSql:" + attachmentSql);
				ret = stmt.executeQuery(attachmentSql);
				JSONArray atts = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//附件id
					obj.put("Id", ret.getString("FILE_URL"));
					//附件名称
					obj.put("Attachment", ret.getString("FILENAME"));
					obj.put("Remark", "");
					atts.add(obj);
				}
				System.out.println("[syncGZ]atts:" + atts.toJSONString());

				//将附件放入数组中
				list.getJSONObject(i).put("AttachmentList", atts);
				//查询评论信息
				String commentSql = "select c.id id,m.code Processedby,a.receive_time ReceivedTime,c.CREATE_DATE ProcessedTime,c.EXT_ATT4 Approved,c.content Comments " +
						" from CTP_COMMENT_ALL c,ORG_MEMBER m,CTP_AFFAIR a " +
						" where m.id=c.CREATE_ID and a.id=c.AFFAIR_ID  and a.OBJECT_ID= " + item.getString("oa_id") +
						" order by a.CREATE_DATE";
				System.out.println("[syncGZ]commentSql:" + commentSql);
				ret = stmt.executeQuery(commentSql);
				JSONArray commons = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//人员编号
					obj.put("Processedby", ret.getString("Processedby") == null ? "" : ret.getString("Processedby"));
					//个人事项创建时间
					obj.put("ReceivedTime", ret.getString("ReceivedTime") == null ? "" : ret.getString("ReceivedTime"));
					//评论创建时间
					obj.put("ProcessedTime", ret.getString("ProcessedTime") == null ? "" : ret.getString("ProcessedTime"));
					//扩展字段 不知存的什么
					obj.put("Approved", ret.getString("Approved") == null ? "801" : (ret.getString("Approved").equals("disagree") ? "802" : "801"));
					//评论恢复信息
					obj.put("Comments", ret.getString("Comments") == null ? "" : ret.getString("Comments"));
					commons.add(obj);
				}
				System.out.println("[syncGZ]commons:" + commons.toJSONString());

				//将评论信息放入数组中
				list.getJSONObject(i).put("ProcessList", commons);
				//查询it选型
				String itsql = "select " +
						OAConfig.SON_ItxhGZ + " xh," +
						OAConfig.SON_ItxxGZ + " xx " +
						"from " + OAConfig.cg_sontableITGZ + " where formmain_id = " + item.getString("oa_mainId");
				System.out.println("[syncGZ]同步广州 itsql = " + itsql);
				ret = stmt.executeQuery(itsql);

				JSONObject itjson = new JSONObject();
				while (ret.next()) {
					if(ret.getString("xh") != null){
						itjson.put(ret.getString("xh"), ret.getString("xx") == null ? "" : ret.getString("xx"));
					}
				}


				//查询formson_0538表 应该是子表
				String sql2 = "select " +
						OAConfig.SON_ItemNoGZ + " ItemNo," +
						OAConfig.SON_InvestmentNoGZ + " InvestmentNo," +
						OAConfig.SON_BudgetAmountGZ + " BudgetAmount " +
						"from " + OAConfig.cg_sontableGZ + " " +
						"where formmain_id=" + item.getString("oa_mainId");
				System.out.println("[syncGZ]同步广州 sql2 = " + sql2);

				ret = stmt.executeQuery(sql2);
				JSONArray ItemList = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//序号
					obj.put("ItemNo", ret.getString("ItemNo") == null ? "" : ret.getString("ItemNo"));
					//看数据字典
					obj.put("InvestmentNo", ret.getString("InvestmentNo") == null ? "" : ret.getString("InvestmentNo"));
					//看数据字典
					obj.put("BudgetAmount", ret.getString("BudgetAmount") == null ? "" : ret.getString("BudgetAmount"));
					//it选型
					if(!itjson.isEmpty()){
						obj.put("ITSelection", itjson.getString(ret.getString("ItemNo")));
					}
					ItemList.add(obj);
				}
				list.getJSONObject(i).put("ItemList", ItemList);
				System.out.println("[syncGZ]doRequest send:" + list.getJSONObject(i));

				String sql3 = "select a.field0004,b.MEMBER_ID,b.id from "+ OAConfig.cg_tableWH + " a left join CTP_AFFAIR b on a.id = b.FORM_RECORDID where a.id = '" + item.getString("oa_mainId") + "'";
				System.out.println("[syncGZ]同步广州 sql3 = " + sql3);
				ret = stmt.executeQuery(sql3);
				while (ret.next()) {
					creCode = ret.getString("MEMBER_ID");
					appCode = ret.getString("field0004");
				}
				String result = doRequest(list.getJSONObject(i),billCode,affId,appCode,creCode,1);
				System.out.println("[syncGZ]doRequest result:" + result);
				//"+result+"
				stmt.executeUpdate("update col_summary set isSend='1' where id=" + item.getString("oa_id"));
				stmt.executeUpdate("update formmain_1145 set field0082='" + result + "' where id=" + item.getString("oa_mainId"));
			} catch (Exception e) {
				System.out.println("[syncGZ]Exception:" + e.getMessage());
				// TODO: handle exception
				e.printStackTrace();
				stmt.executeUpdate("update col_summary set isSend='1' where id=" + item.getString("oa_id"));
				stmt.executeUpdate("update formmain_1145 set field0082='" + e.getMessage() + "' where id=" + item.getString("oa_mainId"));
			}
		}
	}

	/**
	 * 同步太原
	 *
	 * @param stmt
	 * @throws SQLException
	 */
	public void syncTY(Statement stmt) throws SQLException {
		String sql = "SELECT " +
				"s.id, " +
				"s.state, " +
				"f.id mainId, " +
				"f.* " +
				"FROM col_summary s, " + OAConfig.cg_tableTY + " f " +
				"WHERE s.isSend = 0 " +
				"AND s.state IN (1, 3) " +
				"AND s.FORM_APPID = " + OAConfig.cg_appidTY + " " +
				"AND f.id = s.FORM_RECORDID";
		System.out.println("[syncTY]同步太原 sql = " + sql);
		ResultSet ret = stmt.executeQuery(sql);
		JSONArray list = new JSONArray();
		JSONObject item;
		JSONObject obj;
		while (ret.next()) {
			item = new JSONObject();
			item.put("PRNo", ret.getString(OAConfig.cg_PRNoTY));
			item.put("BU", ret.getString(OAConfig.BUTY));
			item.put("oa_id", ret.getString("id"));
			item.put("oa_mainId", ret.getString("mainId"));
			item.put("oa_state", ret.getString("state"));
			item.put("Capital", (ret.getString(OAConfig.cg_CapitalTY) != null && ret.getString(OAConfig.cg_CapitalTY).equals("固定资产")) ? "true" : "false");
			item.put("InvestmentNo", ret.getString(OAConfig.cg_InvestmentNoTY) == null ? "" : ret.getString(OAConfig.cg_InvestmentNoTY));
			item.put("BudgetAmount", ret.getString(OAConfig.cg_BudgetAmountTY) == null ? "" : ret.getString(OAConfig.cg_BudgetAmountTY));
			item.put("SecurityCode", ret.getString(OAConfig.cg_SecurityCodeTY) == null ? "" : ret.getString(OAConfig.cg_SecurityCodeTY));
			item.put("attachment", ret.getString(OAConfig.cg_AttachmentTY) == null ? "0" : ret.getString(OAConfig.cg_AttachmentTY));
			//将查询到的数据存入数组中
			list.add(item);
		}
		System.out.println("[syncTY]list：" + list.toJSONString());
		System.out.println("[syncTY]size:" + list.size());

		//循环数组
		for (int i = 0; i < list.size(); i++) {
			item = list.getJSONObject(i);
			String billCode = item.getString("PRNo");
			String affId = item.getString("oa_id");
			String appCode = "";
			String creCode = "";
			try {
				//查询附件
				String attachmentSql = "select FILE_URL,FILENAME from CTP_ATTACHMENT where SUB_REFERENCE in (" + item.getString("oa_id") + "," + item.getString("attachment") + ")";
				System.out.println("[syncTY]attachmentSql:" + attachmentSql);
				ret = stmt.executeQuery(attachmentSql);
				JSONArray atts = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//附件id
					obj.put("Id", ret.getString("FILE_URL"));
					//附件名称
					obj.put("Attachment", ret.getString("FILENAME"));
					obj.put("Remark", "");
					atts.add(obj);
				}
				System.out.println("[syncTY]atts:" + atts.toJSONString());

				//将附件放入数组中
				list.getJSONObject(i).put("AttachmentList", atts);
				//查询评论信息
				String commentSql = "select c.id id,m.code Processedby,a.receive_time ReceivedTime,c.CREATE_DATE ProcessedTime,c.EXT_ATT4 Approved,c.content Comments " +
						" from CTP_COMMENT_ALL c,ORG_MEMBER m,CTP_AFFAIR a " +
						" where m.id=c.CREATE_ID and a.id=c.AFFAIR_ID  and a.OBJECT_ID= " + item.getString("oa_id") +
						" order by a.CREATE_DATE";
				System.out.println("[syncTY]commentSql:" + commentSql);
				ret = stmt.executeQuery(commentSql);
				JSONArray commons = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//人员编号
					obj.put("Processedby", ret.getString("Processedby") == null ? "" : ret.getString("Processedby"));
					//个人事项创建时间
					obj.put("ReceivedTime", ret.getString("ReceivedTime") == null ? "" : ret.getString("ReceivedTime"));
					//评论创建时间
					obj.put("ProcessedTime", ret.getString("ProcessedTime") == null ? "" : ret.getString("ProcessedTime"));
					//扩展字段 不知存的什么
					obj.put("Approved", ret.getString("Approved") == null ? "801" : (ret.getString("Approved").equals("disagree") ? "802" : "801"));
					//评论恢复信息
					obj.put("Comments", ret.getString("Comments") == null ? "" : ret.getString("Comments"));
					commons.add(obj);
				}
				System.out.println("[syncTY]commons:" + commons.toJSONString());

				//将评论信息放入数组中
				list.getJSONObject(i).put("ProcessList", commons);
				//查询it选型
				String itsql = "select " +
						OAConfig.SON_ItxhTY + " xh," +
						OAConfig.SON_ItxxTY + " xx " +
						"from " + OAConfig.cg_sontableITTY + " where formmain_id = " + item.getString("oa_mainId");
				System.out.println("[syncTY]同步太原 itsql = " + itsql);
				ret = stmt.executeQuery(itsql);

				JSONObject itjson = new JSONObject();
				while (ret.next()) {
					if(ret.getString("xh") != null){
						itjson.put(ret.getString("xh"), ret.getString("xx") == null ? "" : ret.getString("xx"));
					}
				}


				//查询formson_0538表 应该是子表
				String sql2 = "select " +
						OAConfig.SON_ItemNoTY + " ItemNo," +
						OAConfig.SON_InvestmentNoTY + " InvestmentNo," +
						OAConfig.SON_BudgetAmountTY + " BudgetAmount " +
						"from " + OAConfig.cg_sontableTY + " " +
						"where formmain_id=" + item.getString("oa_mainId");
				System.out.println("[syncTY]同步太原 sql2 = " + sql2);

				ret = stmt.executeQuery(sql2);
				JSONArray ItemList = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//序号
					obj.put("ItemNo", ret.getString("ItemNo") == null ? "" : ret.getString("ItemNo"));
					//看数据字典
					obj.put("InvestmentNo", ret.getString("InvestmentNo") == null ? "" : ret.getString("InvestmentNo"));
					//看数据字典
					obj.put("BudgetAmount", ret.getString("BudgetAmount") == null ? "" : ret.getString("BudgetAmount"));
					//it选型
					if(!itjson.isEmpty()){
						obj.put("ITSelection", itjson.getString(ret.getString("ItemNo")));
					}
					ItemList.add(obj);
				}
				list.getJSONObject(i).put("ItemList", ItemList);
				System.out.println("[syncTY]doRequest send:" + list.getJSONObject(i));

				String sql3 = "select a.field0004,b.MEMBER_ID,b.id from "+ OAConfig.cg_tableWH + " a left join CTP_AFFAIR b on a.id = b.FORM_RECORDID where a.id = '" + item.getString("oa_mainId") + "'";
				System.out.println("[syncTY]同步太原 sql3 = " + sql3);
				ret = stmt.executeQuery(sql3);
				while (ret.next()) {
					creCode = ret.getString("MEMBER_ID");
					appCode = ret.getString("field0004");
				}
				String result = doRequest(list.getJSONObject(i),billCode,affId,appCode,creCode,1);
				System.out.println("[syncTY]doRequest result:" + result);
				//"+result+"
				stmt.executeUpdate("update col_summary set isSend='1' where id=" + item.getString("oa_id"));
				stmt.executeUpdate("update formmain_1145 set field0082='" + result + "' where id=" + item.getString("oa_mainId"));
			} catch (Exception e) {
				System.out.println("[syncTY]Exception:" + e.getMessage());
				// TODO: handle exception
				e.printStackTrace();
				stmt.executeUpdate("update col_summary set isSend='1' where id=" + item.getString("oa_id"));
				stmt.executeUpdate("update formmain_1145 set field0082='" + e.getMessage() + "' where id=" + item.getString("oa_mainId"));
			}
		}
	}
	/**
	 * 同步沈阳
	 *
	 * @param stmt
	 * @throws SQLException
	 */
	public void syncSY(Statement stmt) throws SQLException {
		String sql = "SELECT " +
				"s.id, " +
				"s.state, " +
				"f.id mainId, " +
				"f.* " +
				"FROM col_summary s, " + OAConfig.cg_tableSY + " f " +
				"WHERE s.isSend = 0 " +
				"AND s.state IN (1, 3) " +
				"AND s.FORM_APPID = " + OAConfig.cg_appidSY + " " +
				"AND f.id = s.FORM_RECORDID";
		System.out.println("[syncSY]同步沈阳 sql = " + sql);
		ResultSet ret = stmt.executeQuery(sql);
		JSONArray list = new JSONArray();
		JSONObject item;
		JSONObject obj;
		while (ret.next()) {
			item = new JSONObject();
			item.put("PRNo", ret.getString(OAConfig.cg_PRNoSY));
			item.put("BU", ret.getString(OAConfig.BUSY));
			item.put("oa_id", ret.getString("id"));
			item.put("oa_mainId", ret.getString("mainId"));
			item.put("oa_state", ret.getString("state"));
			item.put("Capital", (ret.getString(OAConfig.cg_CapitalSY) != null && ret.getString(OAConfig.cg_CapitalSY).equals("固定资产")) ? "true" : "false");
			item.put("InvestmentNo", ret.getString(OAConfig.cg_InvestmentNoSY) == null ? "" : ret.getString(OAConfig.cg_InvestmentNoSY));
			item.put("BudgetAmount", ret.getString(OAConfig.cg_BudgetAmountSY) == null ? "" : ret.getString(OAConfig.cg_BudgetAmountSY));
			item.put("SecurityCode", ret.getString(OAConfig.cg_SecurityCodeSY) == null ? "" : ret.getString(OAConfig.cg_SecurityCodeSY));
			item.put("attachment", ret.getString(OAConfig.cg_AttachmentSY) == null ? "0" : ret.getString(OAConfig.cg_AttachmentSY));
			//将查询到的数据存入数组中
			list.add(item);
		}
		System.out.println("[syncSY]list：" + list.toJSONString());
		System.out.println("[syncSY]size:" + list.size());

		//循环数组
		for (int i = 0; i < list.size(); i++) {
			item = list.getJSONObject(i);
			String billCode = item.getString("PRNo");
			String affId = item.getString("oa_id");
			String appCode = "";
			String creCode = "";
			try {
				//查询附件
				String attachmentSql = "select FILE_URL,FILENAME from CTP_ATTACHMENT where SUB_REFERENCE in (" + item.getString("oa_id") + "," + item.getString("attachment") + ")";
				System.out.println("[syncSY]attachmentSql:" + attachmentSql);
				ret = stmt.executeQuery(attachmentSql);
				JSONArray atts = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//附件id
					obj.put("Id", ret.getString("FILE_URL"));
					//附件名称
					obj.put("Attachment", ret.getString("FILENAME"));
					obj.put("Remark", "");
					atts.add(obj);
				}
				System.out.println("[syncSY]atts:" + atts.toJSONString());

				//将附件放入数组中
				list.getJSONObject(i).put("AttachmentList", atts);
				//查询评论信息
				String commentSql = "select c.id id,m.code Processedby,a.receive_time ReceivedTime,c.CREATE_DATE ProcessedTime,c.EXT_ATT4 Approved,c.content Comments " +
						" from CTP_COMMENT_ALL c,ORG_MEMBER m,CTP_AFFAIR a " +
						" where m.id=c.CREATE_ID and a.id=c.AFFAIR_ID  and a.OBJECT_ID= " + item.getString("oa_id") +
						" order by a.CREATE_DATE";
				System.out.println("[syncSY]commentSql:" + commentSql);
				ret = stmt.executeQuery(commentSql);
				JSONArray commons = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//人员编号
					obj.put("Processedby", ret.getString("Processedby") == null ? "" : ret.getString("Processedby"));
					//个人事项创建时间
					obj.put("ReceivedTime", ret.getString("ReceivedTime") == null ? "" : ret.getString("ReceivedTime"));
					//评论创建时间
					obj.put("ProcessedTime", ret.getString("ProcessedTime") == null ? "" : ret.getString("ProcessedTime"));
					//扩展字段 不知存的什么
					obj.put("Approved", ret.getString("Approved") == null ? "801" : (ret.getString("Approved").equals("disagree") ? "802" : "801"));
					//评论恢复信息
					obj.put("Comments", ret.getString("Comments") == null ? "" : ret.getString("Comments"));
					commons.add(obj);
				}
				System.out.println("[syncSY]commons:" + commons.toJSONString());

				//将评论信息放入数组中
				list.getJSONObject(i).put("ProcessList", commons);
				//查询it选型
				String itsql = "select " +
						OAConfig.SON_ItxhSY + " xh," +
						OAConfig.SON_ItxxSY + " xx " +
						"from " + OAConfig.cg_sontableITSY + " where formmain_id = " + item.getString("oa_mainId");
				System.out.println("[syncSY]同步沈阳 itsql = " + itsql);
				ret = stmt.executeQuery(itsql);

				JSONObject itjson = new JSONObject();
				while (ret.next()) {
					if(ret.getString("xh") != null){
						itjson.put(ret.getString("xh"), ret.getString("xx") == null ? "" : ret.getString("xx"));
					}
				}


				//查询formson_0538表 应该是子表
				String sql2 = "select " +
						OAConfig.SON_ItemNoSY + " ItemNo," +
						OAConfig.SON_InvestmentNoSY + " InvestmentNo," +
						OAConfig.SON_BudgetAmountSY + " BudgetAmount " +
						"from " + OAConfig.cg_sontableSY + " " +
						"where formmain_id=" + item.getString("oa_mainId");
				System.out.println("[syncSY]同步沈阳 sql2 = " + sql2);

				ret = stmt.executeQuery(sql2);
				JSONArray ItemList = new JSONArray();
				while (ret.next()) {
					obj = new JSONObject();
					//序号
					obj.put("ItemNo", ret.getString("ItemNo") == null ? "" : ret.getString("ItemNo"));
					//看数据字典
					obj.put("InvestmentNo", ret.getString("InvestmentNo") == null ? "" : ret.getString("InvestmentNo"));
					//看数据字典
					obj.put("BudgetAmount", ret.getString("BudgetAmount") == null ? "" : ret.getString("BudgetAmount"));
					//it选型
					if(!itjson.isEmpty()){
						obj.put("ITSelection", itjson.getString(ret.getString("ItemNo")));
					}
					ItemList.add(obj);
				}
				list.getJSONObject(i).put("ItemList", ItemList);
				System.out.println("[syncSY]doRequest send:" + list.getJSONObject(i));

				String sql3 = "select a.field0004,b.MEMBER_ID,b.id from "+ OAConfig.cg_tableWH + " a left join CTP_AFFAIR b on a.id = b.FORM_RECORDID where a.id = '" + item.getString("oa_mainId") + "'";
				System.out.println("[syncSY]同步沈阳 sql3 = " + sql3);
				ret = stmt.executeQuery(sql3);
				while (ret.next()) {
					creCode = ret.getString("MEMBER_ID");
					appCode = ret.getString("field0004");
				}
				String result = doRequest(list.getJSONObject(i),billCode,affId,appCode,creCode,1);
				System.out.println("[syncSY]doRequest result:" + result);
				//"+result+"
				stmt.executeUpdate("update col_summary set isSend='1' where id=" + item.getString("oa_id"));
				stmt.executeUpdate("update formmain_1145 set field0082='" + result + "' where id=" + item.getString("oa_mainId"));
			} catch (Exception e) {
				System.out.println("[syncSY]Exception:" + e.getMessage());
				// TODO: handle exception
				e.printStackTrace();
				stmt.executeUpdate("update col_summary set isSend='1' where id=" + item.getString("oa_id"));
				stmt.executeUpdate("update formmain_1145 set field0082='" + e.getMessage() + "' where id=" + item.getString("oa_mainId"));
			}
		}
	}


	public String doRequest(JSONObject json,String billCode,String affId,String appCode,String creCode,Integer status) {
		try {
			//中间件接口 采购系统  采购申请被拒绝
			String website = null;
			if(status == 0){
				website = OAConfig.oldCgStopUrl;
			}else if (status == 1){
				website = OAConfig.cgStopUrl;
			}

			if (json.getString("oa_state").equals("3"))
				//采购系统 采购申请审批通过
				website = OAConfig.cgProcessUrl;
			//website="http://test.esupplychain.com.cn/Test_ESC0702P4/OA/ApprovePR.ashx";
			System.out.println("接口地址=" + website);
			HttpClient client = new HttpClient();
			PostMethod method = new PostMethod(website);
			method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
			method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
			RequestEntity se = new StringRequestEntity(json.toJSONString(), "application/json", "UTF-8");
			method.setRequestEntity(se);
			client.executeMethod(method);
			String result = method.getResponseBodyAsString();
			method.releaseConnection();
			System.out.println("接口返回的信息=" + result);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			String oaToken = this.getOAToken();
			JSONObject jsonObject = this.sendTodo(billCode, affId, appCode, creCode);
			System.out.println("发送消息的json=" + jsonObject.toJSONString());
			String sumbit = this.sumbit(jsonObject.toJSONString(), oaToken);
			System.out.println("发送消息返回信息=" + sumbit);
			return e.getMessage();
		}
	}

	/**
	 * 组装数据
	 * @param billCode 单据编号
	 * @param affId 个人代办id
	 * @param appCode 申请人编号
	 * @param creCode 创建人编号
	 * @return 返回组装好的数据
	 */
	private JSONObject sendTodo(String billCode,String affId,String appCode,String creCode){
		List<Long> list = new ArrayList<>();
		list.add(Long.parseLong(appCode));
		list.add(Long.parseLong(creCode));
		list.add(Long.parseLong("2249233081631131811"));
		list.add(Long.parseLong("-5170057886319099398"));
		List<String> urlList = new ArrayList<>();
//		urlList.add("http://10.9.112.227:8999/seeyon/collaboration/collaboration.do?method=summary&openFrom=listDone&summaryId=" + affId + "&_isModalDialog=true");
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("senderLoginName","yysjs");
		jsonObject.put("userIds",list);
		jsonObject.put("content",billCode + "该单据未能正常回传采购系统状态!");
		jsonObject.put("url",urlList);
		return jsonObject;
	}


	/**
	 * 获取oa中token
	 * @return 返回token
	 */
	public static String getOAToken(){
		//信创测试环境
		String url = OAConfig.rest_ip + "/seeyon/rest/token/" + OAConfig.rest_user + "/" + OAConfig.rest_pw + "?loginName=yysjs";
		org.apache.http.client.HttpClient client = HttpClientBuilder.create().build();
		HttpGet request = new HttpGet(url);
		request.addHeader("Accept",
				"application/json");
		String result=null;
		try {
			HttpResponse response = client.execute(request);
			HttpEntity entity = response.getEntity();
			result= EntityUtils.toString(entity,"utf-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		JSONObject jsonObject = JSONObject.parseObject(result);
		String token = jsonObject.getString("id");
		return token;
	}


	/**
	 * 发送消息
	 * @param data 表单数据
	 * @param token 令牌
	 * @return 返回结果
	 */
	public static String sumbit(String data,String token){
		try {
			//提交接口地址
			String url = OAConfig.rest_ip + "/seeyon/rest/message/userId?token=" + token;
			HttpClient client = new HttpClient();
			PostMethod method = new PostMethod(url);
			method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
			RequestEntity se = new StringRequestEntity(data, "application/json", "UTF-8");
			method.setRequestEntity(se);
			client.executeMethod(method);
			String result = method.getResponseBodyAsString();
			method.releaseConnection();
			return result;
		}catch (Exception e){
			e.printStackTrace();
			return e.getMessage();
		}

	}


}
